﻿using System;
using System.Windows.Forms;

namespace DataCenter.Component.Adapter
{
    public partial class HorizontalAdapterEditForm : Form
    {
        private HorizontalAdapterEditForm()
        {
            InitializeComponent();
        }
        public HorizontalAdapterEditForm(HorizontalAdapterEntity entity, bool isModify)
            : this()
        {
            this.IsModify = isModify;
            this.Entity = entity;
        }
        private bool IsModify;
        private HorizontalAdapterEntity Entity;

        private void HorizontalAdapterEditForm_Load(object sender, EventArgs e)
        {
            if (IsModify)
            {
                txtComponentName.Text = Entity.ComponentName;
                txtSqlString.Text = Entity.SqlString;
                txtTimeField.Text = Entity.TimeField;                
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!Check()) return;

            Entity.ComponentName = txtComponentName.Text.Trim();
            Entity.SqlString = txtSqlString.Text.Trim();
            Entity.TimeField = txtTimeField.Text.Trim();

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private bool Check()
        {
            bool checkResult = true;
            string errmsg = "";
            TextBox ctrl = null;

            if (string.IsNullOrEmpty(txtComponentName.Text))
            {
                errmsg = "组件名称不能为空";
                ctrl = txtComponentName;
                checkResult = false;
                goto ShowError;
            }
            if (string.IsNullOrEmpty(txtSqlString.Text))
            {
                errmsg = "SQL语句不能为空";
                ctrl = txtSqlString;
                checkResult = false;
                goto ShowError;
            }
            if (string.IsNullOrEmpty(txtTimeField.Text))
            {
                errmsg = "时间字段名称不能为空";
                ctrl = txtTimeField;
                checkResult = false;
                goto ShowError;
            }

            return true;

        ShowError:
            if (!checkResult)
            {
                MessageBox.Show(errmsg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                if (ctrl != null) ctrl.Focus();
            }
            return checkResult;
        }
    }
}
